<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseAdminController;
use Illuminate\Http\Request;
use App\Service\BasicService;

class BasicSettingController extends BaseAdminController{
    protected $basicService;

    public function _init() {
        $this->basicService = new BasicService();
    }

    public function setting(Request $request){
        if($request->isMethod("GET")){
            $basic_id = $request->input("basic_id");
            $basic = $this->basicService->getBasicName($basic_id);
            $res = $this->basicService->setModel("Admin.BasicSetting")->first(['basic_id'=>$basic_id]);
            if(!$res){
                $res['id'] = "";
                $res['exam_paper'] = "";
                $res['templet'] = "";
                $res['exam_times'] = "";
                $res['open_start_at'] = "";
                $res['open_end_at'] = "";
                $res['view_score'] = "";
            }else{
                $res['open_start_at'] = date("Y-m-d H:i:s",$res['open_start_at']);
                $res['open_end_at'] = date("Y-m-d H:i:s",$res['open_end_at']);
            }
            $templet = [
                [
                    'value'=>'1',
                    'name'=>'模版1'
                ],
                [
                    'value'=>'2',
                    'name'=>'模版2'
                ]
            ];
            return view("admin.basic.setting",compact(['res','templet','basic']));
        }else{
            $data = $request->input();
            $this->validateRequest($request,"create");
            $data['open_start_at'] = strtotime($data['open_start_at']);
            $data['open_end_at'] = strtotime($data['open_end_at']);
            if(!empty($data['id'])){
                $result = $this->basicService->setModel("Admin.BasicSetting")->update($data['id'],$data);
            }else{
                $result = $this->basicService->setModel("Admin.BasicSetting")->create($data);
            }
            if($result){
                return $this->layuiDataSuccess(7000,"","操作成功");
            }else{
                return $this->layuiDataError(7000,"操作失败");
            }
        }
    }
}
